#!/usr/bin/env node
// For: nodeunit

var nodeunit = require('nodeunit'),
    path = require('path');

var argv = require('./runner-options').argv;

if (argv.help) {
  require('optimist').showHelp();
  process.exit();
}

var fileFilter = [];
var testFilter = [];

Error.stackTraceLimit = 100;

if (argv.suites) {
  fileFilter = argv.suites.replace(/\s/g, '').split(',');
}

if (argv.tests) {
  testFilter = argv.tests.replace(/\s/g, '').split(',');
}

var files = [
  "to-port-to-wpts/level2/core.js",
  "to-port-to-wpts/level2/events.js",
  "to-port-to-wpts/level2/html.js",
  "to-port-to-wpts/level2/style.js",
  "to-port-to-wpts/level3/textContent.js",
  "to-port-to-wpts/level3/xpath.js",

  "to-port-to-wpts/class-list.js",
  "to-port-to-wpts/compare-document-position.js",
  "to-port-to-wpts/current-script.js",
  "to-port-to-wpts/dom-implementation.js",
  "to-port-to-wpts/frame.js",
  "to-port-to-wpts/history.js",
  "to-port-to-wpts/htmlanchorelement.js",
  "to-port-to-wpts/htmlcanvaselement.js",
  "to-port-to-wpts/htmlelement.js",
  "to-port-to-wpts/htmlimageelement.js",
  "to-port-to-wpts/htmlinputelement.js",
  "to-port-to-wpts/inline-event-handlers.js",
  "to-port-to-wpts/location.js",
  "to-port-to-wpts/message-event.js",
  "to-port-to-wpts/node-clone-node.js",
  "to-port-to-wpts/node-contains.js",
  "to-port-to-wpts/node-iterator.js",
  "to-port-to-wpts/node-owner-document.js",
  "to-port-to-wpts/node-parent-element.js",
  "to-port-to-wpts/non-document-type-child-node.js",
  "to-port-to-wpts/on-error.js",
  "to-port-to-wpts/parent-node.js",
  "to-port-to-wpts/post-message.js",
  "to-port-to-wpts/query-selector-all.js",
  "to-port-to-wpts/query-selector.js",
  "to-port-to-wpts/script.js",
  "to-port-to-wpts/xhr-file-urls.js",
  "to-port-to-wpts/xhr-requires-server.js",

  "sizzle/index.js"
];

if (fileFilter.length > 0) {
  files = files.filter(function(val) {
   var ok = false;
   fileFilter.forEach(function(v) {
     if (val.indexOf(v) > -1) {
      ok = true;
     }
   });
   return ok;
  });
}


var modules = {};
var modulesToRun = {};

files.map(function (p) {
  var required = require(path.join(__dirname, p));
  var module = required.tests || required;
  var filteredModule = {};

  if (testFilter.length) {
    var moduleKeys = Object.keys(module);
    testFilter.forEach(function(filter) {
      filter = filter.toLowerCase();
      moduleKeys.forEach(function(moduleKey) {
        if (moduleKey.toLowerCase().indexOf(filter) > -1) {
          filteredModule[moduleKey] = module[moduleKey];
        }
      });
    });
    module = filteredModule;
  }

  if (module && Object.keys(module).length > 0) {
    modulesToRun[p] = module;
  }
});

// Make sure that any Error that occurs in a callback is logged.
// This should not be something we would have to do manually, the test library should handle it...
process.on("uncaughtException", function (err) {
  console.error("Uncaught exception: ", err, err.stack);
  process.exit(1); // Uncaught Fatal Exception
});

var reporter = argv.verbose ? nodeunit.reporters.default : nodeunit.reporters.minimal;
reporter.run(modulesToRun, null, function(err) {
  process.exit(err ? 1 : 0);
});
